﻿using System.IO;
using System.Runtime.InteropServices;
using Office = Microsoft.Office.Core;
using OL = Microsoft.Office.Interop.Outlook;

namespace uTILLIty.Outlook.FlexFileAddIn
{
	[ComVisible(true)]
	public class FlexFileRibbonExtensibility : Office.IRibbonExtensibility
	{
		public OL.Application Application { get; set; }
		public MapiFolderManager MapiFolderManager { get; set; }

		public string GetCustomUI(string ribbonID)
		{
			//Return the appropriate Ribbon XML for ribbonID
			switch (ribbonID)
			{
				case "Microsoft.Outlook.Explorer":
					string customUi = GetResourceText<FlexFileRibbonExtensibility>("Explorer.xml");
					return customUi;
				//case "Microsoft.Outlook.Mail.Read":
				//  customUI = GetResourceText(
				//      "uTILLIty.Outlook.FlexFileAddIn.ReadMail.xml");
				//  return customUI;
				//case "Microsoft.Mso.IMLayerUI":
				//  customUI = GetResourceText(
				//      "uTILLIty.Outlook.FlexFileAddIn.ContactCard.xml");
				//  return customUI;
				default:
					return string.Empty;
			}
		}

		public bool OnCheckSelection(Office.IRibbonControl control)
		{
			var selection = control.Context as OL.Selection;
			return !(selection == null || selection.Count == 0);
		}

		private static string GetResourceText<T>(string resourceName)
		{
			var asm = typeof(T).Assembly;
			asm.GetManifestResourceStream(typeof(T), resourceName);
			using (var stream = asm.GetManifestResourceStream(typeof(T), resourceName))
			{
				if (stream == null)
					return null;
				using (var resourceReader = new StreamReader(stream))
				{
					return resourceReader.ReadToEnd();
				}
			}
		}

	}
}